<?php
class LoginAction extends Action {

	public $login_expire = 3600;
	public function __construct(){
		parent::__construct();
	}
	public function index(){
		if(getBrowser()!="Chrome" && getBrowser()!="Firefox"){
			$this->browser = getBrowser();
		}
		if( $_POST["act"] == "login"){
			if(md5($_POST['code']) == session('verify')){
				$Aduser = D('Aduser');
				$data['adname'] = $_POST['username'];
				$data['adpw'] = md5($_POST['password']);
				$AduserArr = $Aduser->where($data)->find();
				if($AduserArr){
					$id = $AduserArr['id'];
					$data1 = array(
								"loginnum"=>$AduserArr['loginnum']+1,
								"lastloginip"=>get_client_ip(),
								"lastlogintime"=>time(),
					         );
					$Aduser->where('id='.$id)->save($data1);
					//用户管理员数组
					unset($AduserArr['adpw']);
					
					$session = array(
								  "sversion"=>SESSION_VERSION,//session版本
								  "user"=>$AduserArr,
								  "username"=>$AduserArr["adname"],
								  "effective"=>time()+60*60,//有效期一个小时
								  "chrome"=>array(
							      "uid"=>$AduserArr['uid'],
								  ),
							    );
					session(getOnlyName("admin"),$session);
					$this->redirect("Index/index");
				}else{
					$this->msg = "提示:帐号或密码有误!";
				}
      		}   
      		else{
				$this->msg = "提示:验证码错误!";   		    
     		}
		}
		$this->display("default:login");//登录页面
	}
	
	/* 快速登录 */
	public function fastlogin(){
		$uid = $_GET['uid'];
		if(empty($uid) || $uid == 0){
			echo "非法操作!";
			return;
		}
		$Aduser = D('Aduser');
		$AduserArr = $Aduser->where("uid = '".$uid."'")->find();
		if($AduserArr){
			$id = $AduserArr['id'];
			$data1 = array(
						"loginnum"=>$AduserArr['loginnum']+1,
						"lastloginip"=>get_client_ip(),
						"lastlogintime"=>time(),
					 );
			$Aduser->where('uid='.$id)->save($data1);
			
			//用户管理员数组
			unset($AduserArr['adpw']);
			
			$session = array(
						  "sversion"=>SESSION_VERSION,//session版本
						  "user"=>$AduserArr,
						  "username"=>$AduserArr["adname"],
						  "effective"=>time()+60*60,//有效期一个小时
						  "chrome"=>array(
						  		"uid"=>$AduserArr['uid'],
						  ),
						);
			session(getOnlyName("admin"),$session);
			
			//用户插件
			$User = D('User');
			$res = $User->where('id = "'.$uid.'"')->find();
			session(getOnlyName("user"),$res);
			
			$this->redirect("Index/index");
		}else{
			echo "操作失效";	
		}
	}
	
	public function verify(){// 生成验证码      
        import("ORG.Util.Image"); 
		 ob_end_clean();  
        Image::buildImageVerify();   
    }
	public function _empty(){   
	  header("HTTP/1.0 404 Not Found"); 
      $this->display('default:error/404');  
	}
}
?>